home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 201 / 201.xpi / components / contentHandling.js < prev    next >
Text File  |  2010-01-11  |  3KB  |  112 lines

  1. /* You may find the license in the LICENSE file */
  2.  
  3. function include(uri) {
  4.     Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  5.         .getService(Components.interfaces.mozIJSSubScriptLoader)
  6.         .loadSubScript(uri);
  7. }
  8. include('chrome://dta/content/common/xpcom.jsm');
  9.  
  10. const NS_ERROR_NO_INTERFACE = Cr.NS_ERROR_NO_INTERFACE;
  11. const NS_ERROR_FAILURE = Cr.NS_ERROR_FAILURE;
  12. const NS_ERROR_NO_AGGREGATION = Cr.NS_ERROR_NO_AGGREGATION;
  13. const NS_ERROR_INVALID_ARG = Cr.NS_ERROR_INVALID_ARG;
  14.  
  15. const ScriptableInputStream = new Components.Constructor('@mozilla.org/scriptableinputstream;1', 'nsIScriptableInputStream', 'init');
  16.  
  17. var ContentHandling = {
  18.     _init: function() {
  19.         var obs = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);
  20.         obs.addObserver(this, 'http-on-modify-request', true);
  21.     },
  22.     observe: function(subject, topic, data) {
  23.         if (
  24.             !(subject instanceof Ci.nsIHttpChannel)
  25.             || !(subject instanceof Ci.nsIUploadChannel)
  26.         ) {
  27.             return;
  28.         }
  29.         var channel = subject.QueryInterface(Ci.nsIHttpChannel);
  30.         if (channel.requestMethod != 'POST') {
  31.             return;
  32.         }
  33.                 
  34.         var post;
  35.     
  36.         try {
  37.             var us = subject.QueryInterface(Ci.nsIUploadChannel).uploadStream;
  38.             if (!us) {
  39.                 return;
  40.             }
  41.             try {
  42.                 us.QueryInterface(Ci.nsIMultiplexInputStream);
  43.                 debug("ignoring multiplex stream");
  44.                 return;
  45.             }
  46.             catch (ex) {
  47.                 // no op
  48.             }
  49.                 
  50.             let ss = us.QueryInterface(Ci.nsISeekableStream);
  51.             if (!ss) {
  52.                 return;
  53.             }
  54.             let op = ss.tell();
  55.         
  56.             ss.seek(0, 0);
  57.             
  58.             let is = new ScriptableInputStream(us);
  59.             
  60.             // we'll read max 64k
  61.             let available = Math.min(is.available(), 1 << 16);
  62.             if (available) {
  63.                 post = is.read(available);
  64.             }
  65.             ss.seek(0, op);
  66.             
  67.             if (post) {
  68.                 this._registerData(channel.URI, post);
  69.             }
  70.         }
  71.         catch (ex) {
  72.             debug("cannot get post-data", ex);
  73.         }
  74.   },
  75.   _dataDict: {},
  76.   _dataArray: [],
  77.   _registerData: function(uri, data) {
  78.       uri = uri.spec;
  79.  
  80.       if (!(uri in this._dataDict)) {
  81.           if (this._dataArray.length > 5) {
  82.               delete this._dataDict[this._dataArray.pop()];
  83.           }
  84.           this._dataArray.push(uri);
  85.       }
  86.       
  87.       this._dataDict[uri] = data;      
  88.   },
  89.   getPostDataFor: function(uri) {
  90.       if (uri instanceof Ci.nsIURI) {
  91.           uri = uri.spec;
  92.       }
  93.       if (!(uri in this._dataDict)) {
  94.           return '';
  95.       }
  96.       return this._dataDict[uri];
  97.   }      
  98. };
  99. implementComponent(
  100.     ContentHandling,
  101.     Components.ID("{47C53284-E2D1-49af-9524-4D42D70D1279}"),
  102.     "@downthemall.net/contenthandling;1",
  103.     "DownThemAll! Content Handling",
  104.     [Ci.nsIObserver, Ci.nsiURIContentListener, Ci.dtaIContentHandling]
  105. );    
  106. ContentHandling._init();
  107.  
  108.  
  109. // entrypoint
  110. function NSGetModule(compMgr, fileSpec) {
  111.     return new ServiceModule(ContentHandling, true);
  112. }